s3: VFS: full_audit.c: Use real dirfsp for SMB_VFS_RENAMEAT()
[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         int saved_errno;
1414         struct smb_filename *full_fname_src = NULL;
1415         struct smb_filename *full_fname_dst = NULL;
1416
1417         full_fname_src = full_path_from_dirfsp_atname(talloc_tos(),
1418                                                       srcfsp,
1419                                                       smb_fname_src);
1420         if (full_fname_src == NULL) {
1421                 errno = ENOMEM;
1422                 return -1;
1423         }
1424         full_fname_dst = full_path_from_dirfsp_atname(talloc_tos(),
1425                                                       dstfsp,
1426                                                       smb_fname_dst);
1427         if (full_fname_dst == NULL) {
1428                 TALLOC_FREE(full_fname_src);
1429                 errno = ENOMEM;
1430                 return -1;
1431         }
1432
1433         result = SMB_VFS_NEXT_RENAMEAT(handle,
1434                                 srcfsp,
1435                                 smb_fname_src,
1436                                 dstfsp,
1437                                 smb_fname_dst);
1438
1439         if (result == -1) {
1440                 saved_errno = errno;
1441         }
1442         do_log(SMB_VFS_OP_RENAMEAT, (result >= 0), handle, "%s|%s",
1443                smb_fname_str_do_log(handle->conn, full_fname_src),
1444                smb_fname_str_do_log(handle->conn, full_fname_dst));
1445
1446         TALLOC_FREE(full_fname_src);
1447         TALLOC_FREE(full_fname_dst);
1448
1449         if (result == -1) {
1450                 errno = saved_errno;
1451         }
1452         return result;
1453 }
1454
1455 struct smb_full_audit_fsync_state {
1456         vfs_handle_struct *handle;
1457         files_struct *fsp;
1458         int ret;
1459         struct vfs_aio_state vfs_aio_state;
1460 };
1461
1462 static void smb_full_audit_fsync_done(struct tevent_req *subreq);
1463
1464 static struct tevent_req *smb_full_audit_fsync_send(
1465         struct vfs_handle_struct *handle, TALLOC_CTX *mem_ctx,
1466         struct tevent_context *ev, struct files_struct *fsp)
1467 {
1468         struct tevent_req *req, *subreq;
1469         struct smb_full_audit_fsync_state *state;
1470
1471         req = tevent_req_create(mem_ctx, &state,
1472                                 struct smb_full_audit_fsync_state);
1473         if (req == NULL) {
1474                 do_log(SMB_VFS_OP_FSYNC_SEND, false, handle, "%s",
1475                        fsp_str_do_log(fsp));
1476                 return NULL;
1477         }
1478         state->handle = handle;
1479         state->fsp = fsp;
1480
1481         subreq = SMB_VFS_NEXT_FSYNC_SEND(state, ev, handle, fsp);
1482         if (tevent_req_nomem(subreq, req)) {
1483                 do_log(SMB_VFS_OP_FSYNC_SEND, false, handle, "%s",
1484                        fsp_str_do_log(fsp));
1485                 return tevent_req_post(req, ev);
1486         }
1487         tevent_req_set_callback(subreq, smb_full_audit_fsync_done, req);
1488
1489         do_log(SMB_VFS_OP_FSYNC_SEND, true, handle, "%s", fsp_str_do_log(fsp));
1490         return req;
1491 }
1492
1493 static void smb_full_audit_fsync_done(struct tevent_req *subreq)
1494 {
1495         struct tevent_req *req = tevent_req_callback_data(
1496                 subreq, struct tevent_req);
1497         struct smb_full_audit_fsync_state *state = tevent_req_data(
1498                 req, struct smb_full_audit_fsync_state);
1499
1500         state->ret = SMB_VFS_FSYNC_RECV(subreq, &state->vfs_aio_state);
1501         TALLOC_FREE(subreq);
1502         tevent_req_done(req);
1503 }
1504
1505 static int smb_full_audit_fsync_recv(struct tevent_req *req,
1506                                      struct vfs_aio_state *vfs_aio_state)
1507 {
1508         struct smb_full_audit_fsync_state *state = tevent_req_data(
1509                 req, struct smb_full_audit_fsync_state);
1510
1511         if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1512                 do_log(SMB_VFS_OP_FSYNC_RECV, false, state->handle, "%s",
1513                        fsp_str_do_log(state->fsp));
1514                 return -1;
1515         }
1516
1517         do_log(SMB_VFS_OP_FSYNC_RECV, (state->ret >= 0), state->handle, "%s",
1518                fsp_str_do_log(state->fsp));
1519
1520         *vfs_aio_state = state->vfs_aio_state;
1521         return state->ret;
1522 }
1523
1524 static int smb_full_audit_stat(vfs_handle_struct *handle,
1525                                struct smb_filename *smb_fname)
1526 {
1527         int result;
1528         
1529         result = SMB_VFS_NEXT_STAT(handle, smb_fname);
1530
1531         do_log(SMB_VFS_OP_STAT, (result >= 0), handle, "%s",
1532                smb_fname_str_do_log(handle->conn, smb_fname));
1533
1534         return result;    
1535 }
1536
1537 static int smb_full_audit_fstat(vfs_handle_struct *handle, files_struct *fsp,
1538                        SMB_STRUCT_STAT *sbuf)
1539 {
1540         int result;
1541         
1542         result = SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
1543
1544         do_log(SMB_VFS_OP_FSTAT, (result >= 0), handle, "%s",
1545                fsp_str_do_log(fsp));
1546
1547         return result;
1548 }
1549
1550 static int smb_full_audit_lstat(vfs_handle_struct *handle,
1551                                 struct smb_filename *smb_fname)
1552 {
1553         int result;
1554         
1555         result = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
1556
1557         do_log(SMB_VFS_OP_LSTAT, (result >= 0), handle, "%s",
1558                smb_fname_str_do_log(handle->conn, smb_fname));
1559
1560         return result;    
1561 }
1562
1563 static uint64_t smb_full_audit_get_alloc_size(vfs_handle_struct *handle,
1564                        files_struct *fsp, const SMB_STRUCT_STAT *sbuf)
1565 {
1566         uint64_t result;
1567
1568         result = SMB_VFS_NEXT_GET_ALLOC_SIZE(handle, fsp, sbuf);
1569
1570         do_log(SMB_VFS_OP_GET_ALLOC_SIZE, (result != (uint64_t)-1), handle,
1571                         "%llu", (unsigned long long)result);
1572
1573         return result;
1574 }
1575
1576 static int smb_full_audit_unlinkat(vfs_handle_struct *handle,
1577                         struct files_struct *dirfsp,
1578                         const struct smb_filename *smb_fname,
1579                         int flags)
1580 {
1581         struct smb_filename *full_fname = NULL;
1582         int result;
1583
1584         full_fname = full_path_from_dirfsp_atname(talloc_tos(),
1585                                                   dirfsp,
1586                                                   smb_fname);
1587         if (full_fname == NULL) {
1588                 return -1;
1589         }
1590
1591         result = SMB_VFS_NEXT_UNLINKAT(handle,
1592                         dirfsp,
1593                         smb_fname,
1594                         flags);
1595
1596         do_log(SMB_VFS_OP_UNLINKAT, (result >= 0), handle, "%s",
1597                smb_fname_str_do_log(handle->conn, full_fname));
1598
1599         TALLOC_FREE(full_fname);
1600         return result;
1601 }
1602
1603 static int smb_full_audit_fchmod(vfs_handle_struct *handle, files_struct *fsp,
1604                         mode_t mode)
1605 {
1606         int result;
1607         
1608         result = SMB_VFS_NEXT_FCHMOD(handle, fsp, mode);
1609
1610         do_log(SMB_VFS_OP_FCHMOD, (result >= 0), handle,
1611                "%s|%o", fsp_str_do_log(fsp), mode);
1612
1613         return result;
1614 }
1615
1616 static int smb_full_audit_fchown(vfs_handle_struct *handle, files_struct *fsp,
1617                         uid_t uid, gid_t gid)
1618 {
1619         int result;
1620
1621         result = SMB_VFS_NEXT_FCHOWN(handle, fsp, uid, gid);
1622
1623         do_log(SMB_VFS_OP_FCHOWN, (result >= 0), handle, "%s|%ld|%ld",
1624                fsp_str_do_log(fsp), (long int)uid, (long int)gid);
1625
1626         return result;
1627 }
1628
1629 static int smb_full_audit_lchown(vfs_handle_struct *handle,
1630                         const struct smb_filename *smb_fname,
1631                         uid_t uid,
1632                         gid_t gid)
1633 {
1634         int result;
1635
1636         result = SMB_VFS_NEXT_LCHOWN(handle, smb_fname, uid, gid);
1637
1638         do_log(SMB_VFS_OP_LCHOWN, (result >= 0), handle, "%s|%ld|%ld",
1639                smb_fname->base_name, (long int)uid, (long int)gid);
1640
1641         return result;
1642 }
1643
1644 static int smb_full_audit_chdir(vfs_handle_struct *handle,
1645                         const struct smb_filename *smb_fname)
1646 {
1647         int result;
1648
1649         result = SMB_VFS_NEXT_CHDIR(handle, smb_fname);
1650
1651         do_log(SMB_VFS_OP_CHDIR,
1652                (result >= 0),
1653                handle,
1654                "chdir|%s",
1655                smb_fname_str_do_log(handle->conn, smb_fname));
1656
1657         return result;
1658 }
1659
1660 static struct smb_filename *smb_full_audit_getwd(vfs_handle_struct *handle,
1661                                 TALLOC_CTX *ctx)
1662 {
1663         struct smb_filename *result;
1664
1665         result = SMB_VFS_NEXT_GETWD(handle, ctx);
1666         
1667         do_log(SMB_VFS_OP_GETWD, (result != NULL), handle, "%s",
1668                 result == NULL? "" : result->base_name);
1669
1670         return result;
1671 }
1672
1673 static int smb_full_audit_fntimes(vfs_handle_struct *handle,
1674                                   files_struct *fsp,
1675                                   struct smb_file_time *ft)
1676 {
1677         int result;
1678         time_t create_time = convert_timespec_to_time_t(ft->create_time);
1679         time_t atime = convert_timespec_to_time_t(ft->atime);
1680         time_t mtime = convert_timespec_to_time_t(ft->mtime);
1681         time_t ctime = convert_timespec_to_time_t(ft->ctime);
1682         const char *create_time_str = "";
1683         const char *atime_str = "";
1684         const char *mtime_str = "";
1685         const char *ctime_str = "";
1686         TALLOC_CTX *frame = talloc_stackframe();
1687
1688         if (frame == NULL) {
1689                 errno = ENOMEM;
1690                 return -1;
1691         }
1692
1693         result = SMB_VFS_NEXT_FNTIMES(handle, fsp, ft);
1694
1695         if (create_time > 0) {
1696                 create_time_str = timestring(frame, create_time);
1697         }
1698         if (atime > 0) {
1699                 atime_str = timestring(frame, atime);
1700         }
1701         if (mtime > 0) {
1702                 mtime_str = timestring(frame, mtime);
1703         }
1704         if (ctime > 0) {
1705                 ctime_str = timestring(frame, ctime);
1706         }
1707
1708         do_log(SMB_VFS_OP_FNTIMES,
1709                (result >= 0),
1710                handle,
1711                "%s|%s|%s|%s|%s",
1712                fsp_str_do_log(fsp),
1713                create_time_str,
1714                atime_str,
1715                mtime_str,
1716                ctime_str);
1717
1718         TALLOC_FREE(frame);
1719
1720         return result;
1721 }
1722
1723 static int smb_full_audit_ftruncate(vfs_handle_struct *handle, files_struct *fsp,
1724                            off_t len)
1725 {
1726         int result;
1727
1728         result = SMB_VFS_NEXT_FTRUNCATE(handle, fsp, len);
1729
1730         do_log(SMB_VFS_OP_FTRUNCATE, (result >= 0), handle,
1731                "%s", fsp_str_do_log(fsp));
1732
1733         return result;
1734 }
1735
1736 static int smb_full_audit_fallocate(vfs_handle_struct *handle, files_struct *fsp,
1737                            uint32_t mode,
1738                            off_t offset,
1739                            off_t len)
1740 {
1741         int result;
1742
1743         result = SMB_VFS_NEXT_FALLOCATE(handle, fsp, mode, offset, len);
1744
1745         do_log(SMB_VFS_OP_FALLOCATE, (result >= 0), handle,
1746                "%s", fsp_str_do_log(fsp));
1747
1748         return result;
1749 }
1750
1751 static bool smb_full_audit_lock(vfs_handle_struct *handle, files_struct *fsp,
1752                        int op, off_t offset, off_t count, int type)
1753 {
1754         bool result;
1755
1756         result = SMB_VFS_NEXT_LOCK(handle, fsp, op, offset, count, type);
1757
1758         do_log(SMB_VFS_OP_LOCK, result, handle, "%s", fsp_str_do_log(fsp));
1759
1760         return result;
1761 }
1762
1763 static int smb_full_audit_kernel_flock(struct vfs_handle_struct *handle,
1764                                        struct files_struct *fsp,
1765                                        uint32_t share_access,
1766                                        uint32_t access_mask)
1767 {
1768         int result;
1769
1770         result = SMB_VFS_NEXT_KERNEL_FLOCK(handle,
1771                                            fsp,
1772                                            share_access,
1773                                            access_mask);
1774
1775         do_log(SMB_VFS_OP_KERNEL_FLOCK, (result >= 0), handle, "%s",
1776                fsp_str_do_log(fsp));
1777
1778         return result;
1779 }
1780
1781 static int smb_full_audit_fcntl(struct vfs_handle_struct *handle,
1782                                 struct files_struct *fsp,
1783                                 int cmd, va_list cmd_arg)
1784 {
1785         void *arg;
1786         va_list dup_cmd_arg;
1787         int result;
1788
1789         va_copy(dup_cmd_arg, cmd_arg);
1790         arg = va_arg(dup_cmd_arg, void *);
1791         result = SMB_VFS_NEXT_FCNTL(handle, fsp, cmd, arg);
1792         va_end(dup_cmd_arg);
1793
1794         do_log(SMB_VFS_OP_FCNTL, (result >= 0), handle, "%s",
1795                fsp_str_do_log(fsp));
1796
1797         return result;
1798 }
1799
1800 static int smb_full_audit_linux_setlease(vfs_handle_struct *handle, files_struct *fsp,
1801                                  int leasetype)
1802 {
1803         int result;
1804
1805         result = SMB_VFS_NEXT_LINUX_SETLEASE(handle, fsp, leasetype);
1806
1807         do_log(SMB_VFS_OP_LINUX_SETLEASE, (result >= 0), handle, "%s",
1808                fsp_str_do_log(fsp));
1809
1810         return result;
1811 }
1812
1813 static bool smb_full_audit_getlock(vfs_handle_struct *handle, files_struct *fsp,
1814                        off_t *poffset, off_t *pcount, int *ptype, pid_t *ppid)
1815 {
1816         bool result;
1817
1818         result = SMB_VFS_NEXT_GETLOCK(handle, fsp, poffset, pcount, ptype, ppid);
1819
1820         do_log(SMB_VFS_OP_GETLOCK, result, handle, "%s", fsp_str_do_log(fsp));
1821
1822         return result;
1823 }
1824
1825 static int smb_full_audit_symlinkat(vfs_handle_struct *handle,
1826                         const struct smb_filename *link_contents,
1827                         struct files_struct *dirfsp,
1828                         const struct smb_filename *new_smb_fname)
1829 {
1830         struct smb_filename *full_fname = NULL;
1831         int result;
1832
1833         full_fname = full_path_from_dirfsp_atname(talloc_tos(),
1834                                                 dirfsp,
1835                                                 new_smb_fname);
1836         if (full_fname == NULL) {
1837                 return -1;
1838         }
1839
1840         result = SMB_VFS_NEXT_SYMLINKAT(handle,
1841                                 link_contents,
1842                                 dirfsp,
1843                                 new_smb_fname);
1844
1845         do_log(SMB_VFS_OP_SYMLINKAT,
1846                (result >= 0),
1847                handle,
1848                "%s|%s",
1849                link_contents->base_name,
1850                smb_fname_str_do_log(handle->conn, full_fname));
1851
1852         TALLOC_FREE(full_fname);
1853
1854         return result;
1855 }
1856
1857 static int smb_full_audit_readlinkat(vfs_handle_struct *handle,
1858                         const struct files_struct *dirfsp,
1859                         const struct smb_filename *smb_fname,
1860                         char *buf,
1861                         size_t bufsiz)
1862 {
1863         struct smb_filename *full_fname = NULL;
1864         int result;
1865
1866         full_fname = full_path_from_dirfsp_atname(talloc_tos(),
1867                                                 dirfsp,
1868                                                 smb_fname);
1869         if (full_fname == NULL) {
1870                 return -1;
1871         }
1872
1873         result = SMB_VFS_NEXT_READLINKAT(handle,
1874                         dirfsp,
1875                         smb_fname,
1876                         buf,
1877                         bufsiz);
1878
1879         do_log(SMB_VFS_OP_READLINKAT,
1880                (result >= 0),
1881                handle,
1882                "%s",
1883                smb_fname_str_do_log(handle->conn, full_fname));
1884
1885         TALLOC_FREE(full_fname);
1886
1887         return result;
1888 }
1889
1890 static int smb_full_audit_linkat(vfs_handle_struct *handle,
1891                         files_struct *srcfsp,
1892                         const struct smb_filename *old_smb_fname,
1893                         files_struct *dstfsp,
1894                         const struct smb_filename *new_smb_fname,
1895                         int flags)
1896 {
1897         struct smb_filename *old_full_fname = NULL;
1898         struct smb_filename *new_full_fname = NULL;
1899         int result;
1900
1901         old_full_fname = full_path_from_dirfsp_atname(talloc_tos(),
1902                                                 srcfsp,
1903                                                 old_smb_fname);
1904         if (old_full_fname == NULL) {
1905                 return -1;
1906         }
1907         new_full_fname = full_path_from_dirfsp_atname(talloc_tos(),
1908                                                 dstfsp,
1909                                                 new_smb_fname);
1910         if (new_full_fname == NULL) {
1911                 TALLOC_FREE(old_full_fname);
1912                 return -1;
1913         }
1914         result = SMB_VFS_NEXT_LINKAT(handle,
1915                         srcfsp,
1916                         old_smb_fname,
1917                         dstfsp,
1918                         new_smb_fname,
1919                         flags);
1920
1921         do_log(SMB_VFS_OP_LINKAT,
1922                (result >= 0),
1923                handle,
1924                "%s|%s",
1925                smb_fname_str_do_log(handle->conn, old_full_fname),
1926                smb_fname_str_do_log(handle->conn, new_full_fname));
1927
1928         TALLOC_FREE(old_full_fname);
1929         TALLOC_FREE(new_full_fname);
1930
1931         return result;
1932 }
1933
1934 static int smb_full_audit_mknodat(vfs_handle_struct *handle,
1935                         files_struct *dirfsp,
1936                         const struct smb_filename *smb_fname,
1937                         mode_t mode,
1938                         SMB_DEV_T dev)
1939 {
1940         struct smb_filename *full_fname = NULL;
1941         int result;
1942
1943         full_fname = full_path_from_dirfsp_atname(talloc_tos(),
1944                                                 dirfsp,
1945                                                 smb_fname);
1946         if (full_fname == NULL) {
1947                 return -1;
1948         }
1949
1950         result = SMB_VFS_NEXT_MKNODAT(handle,
1951                                 dirfsp,
1952                                 smb_fname,
1953                                 mode,
1954                                 dev);
1955
1956         do_log(SMB_VFS_OP_MKNODAT,
1957                (result >= 0),
1958                handle,
1959                "%s",
1960                smb_fname_str_do_log(handle->conn, full_fname));
1961
1962         TALLOC_FREE(full_fname);
1963
1964         return result;
1965 }
1966
1967 static struct smb_filename *smb_full_audit_realpath(vfs_handle_struct *handle,
1968                                 TALLOC_CTX *ctx,
1969                                 const struct smb_filename *smb_fname)
1970 {
1971         struct smb_filename *result_fname = NULL;
1972
1973         result_fname = SMB_VFS_NEXT_REALPATH(handle, ctx, smb_fname);
1974
1975         do_log(SMB_VFS_OP_REALPATH,
1976                (result_fname != NULL),
1977                handle,
1978                "%s",
1979                smb_fname_str_do_log(handle->conn, smb_fname));
1980
1981         return result_fname;
1982 }
1983
1984 static int smb_full_audit_chflags(vfs_handle_struct *handle,
1985                         const struct smb_filename *smb_fname,
1986                         unsigned int flags)
1987 {
1988         int result;
1989
1990         result = SMB_VFS_NEXT_CHFLAGS(handle, smb_fname, flags);
1991
1992         do_log(SMB_VFS_OP_CHFLAGS,
1993                (result != 0),
1994                handle,
1995                "%s",
1996                smb_fname_str_do_log(handle->conn, smb_fname));
1997
1998         return result;
1999 }
2000
2001 static struct file_id smb_full_audit_file_id_create(struct vfs_handle_struct *handle,
2002                                                     const SMB_STRUCT_STAT *sbuf)
2003 {
2004         struct file_id id_zero = { 0 };
2005         struct file_id result;
2006         struct file_id_buf idbuf;
2007
2008         result = SMB_VFS_NEXT_FILE_ID_CREATE(handle, sbuf);
2009
2010         do_log(SMB_VFS_OP_FILE_ID_CREATE,
2011                !file_id_equal(&id_zero, &result),
2012                handle,
2013                "%s",
2014                file_id_str_buf(result, &idbuf));
2015
2016         return result;
2017 }
2018
2019 static uint64_t smb_full_audit_fs_file_id(struct vfs_handle_struct *handle,
2020                                           const SMB_STRUCT_STAT *sbuf)
2021 {
2022         uint64_t result;
2023
2024         result = SMB_VFS_NEXT_FS_FILE_ID(handle, sbuf);
2025
2026         do_log(SMB_VFS_OP_FS_FILE_ID,
2027                result != 0,
2028                handle, "%" PRIu64, result);
2029
2030         return result;
2031 }
2032
2033 static NTSTATUS smb_full_audit_fstreaminfo(vfs_handle_struct *handle,
2034                                           struct files_struct *fsp,
2035                                           TALLOC_CTX *mem_ctx,
2036                                           unsigned int *pnum_streams,
2037                                           struct stream_struct **pstreams)
2038 {
2039         NTSTATUS result;
2040
2041         result = SMB_VFS_NEXT_FSTREAMINFO(handle, fsp, mem_ctx,
2042                                          pnum_streams, pstreams);
2043
2044         do_log(SMB_VFS_OP_FSTREAMINFO,
2045                NT_STATUS_IS_OK(result),
2046                handle,
2047                "%s",
2048                smb_fname_str_do_log(handle->conn, fsp->fsp_name));
2049
2050         return result;
2051 }
2052
2053 static int smb_full_audit_get_real_filename(struct vfs_handle_struct *handle,
2054                                             const struct smb_filename *path,
2055                                             const char *name,
2056                                             TALLOC_CTX *mem_ctx,
2057                                             char **found_name)
2058 {
2059         int result;
2060
2061         result = SMB_VFS_NEXT_GET_REAL_FILENAME(handle, path, name, mem_ctx,
2062                                                 found_name);
2063
2064         do_log(SMB_VFS_OP_GET_REAL_FILENAME, (result == 0), handle,
2065                "%s/%s->%s",
2066                path->base_name, name, (result == 0) ? *found_name : "");
2067
2068         return result;
2069 }
2070
2071 static const char *smb_full_audit_connectpath(vfs_handle_struct *handle,
2072                                         const struct smb_filename *smb_fname)
2073 {
2074         const char *result;
2075
2076         result = SMB_VFS_NEXT_CONNECTPATH(handle, smb_fname);
2077
2078         do_log(SMB_VFS_OP_CONNECTPATH,
2079                result != NULL,
2080                handle,
2081                "%s",
2082                smb_fname_str_do_log(handle->conn, smb_fname));
2083
2084         return result;
2085 }
2086
2087 static NTSTATUS smb_full_audit_brl_lock_windows(struct vfs_handle_struct *handle,
2088                                                 struct byte_range_lock *br_lck,
2089                                                 struct lock_struct *plock)
2090 {
2091         NTSTATUS result;
2092
2093         result = SMB_VFS_NEXT_BRL_LOCK_WINDOWS(handle, br_lck, plock);
2094
2095         do_log(SMB_VFS_OP_BRL_LOCK_WINDOWS, NT_STATUS_IS_OK(result), handle,
2096             "%s:%llu-%llu. type=%d.",
2097                fsp_str_do_log(brl_fsp(br_lck)),
2098                (unsigned long long)plock->start,
2099                (unsigned long long)plock->size,
2100                plock->lock_type);
2101
2102         return result;
2103 }
2104
2105 static bool smb_full_audit_brl_unlock_windows(struct vfs_handle_struct *handle,
2106                                               struct byte_range_lock *br_lck,
2107                                               const struct lock_struct *plock)
2108 {
2109         bool result;
2110
2111         result = SMB_VFS_NEXT_BRL_UNLOCK_WINDOWS(handle, br_lck, plock);
2112
2113         do_log(SMB_VFS_OP_BRL_UNLOCK_WINDOWS, (result == 0), handle,
2114                "%s:%llu-%llu:%d", fsp_str_do_log(brl_fsp(br_lck)),
2115                (unsigned long long)plock->start,
2116                (unsigned long long)plock->size,
2117                plock->lock_type);
2118
2119         return result;
2120 }
2121
2122 static bool smb_full_audit_strict_lock_check(struct vfs_handle_struct *handle,
2123                                              struct files_struct *fsp,
2124                                              struct lock_struct *plock)
2125 {
2126         bool result;
2127
2128         result = SMB_VFS_NEXT_STRICT_LOCK_CHECK(handle, fsp, plock);
2129
2130         do_log(SMB_VFS_OP_STRICT_LOCK_CHECK, result, handle,
2131                "%s:%llu-%llu:%d", fsp_str_do_log(fsp),
2132                (unsigned long long)plock->start,
2133                (unsigned long long)plock->size,
2134                plock->lock_type);
2135
2136         return result;
2137 }
2138
2139 static NTSTATUS smb_full_audit_translate_name(struct vfs_handle_struct *handle,
2140                                               const char *name,
2141                                               enum vfs_translate_direction direction,
2142                                               TALLOC_CTX *mem_ctx,
2143                                               char **mapped_name)
2144 {
2145         NTSTATUS result;
2146
2147         result = SMB_VFS_NEXT_TRANSLATE_NAME(handle, name, direction, mem_ctx,
2148                                              mapped_name);
2149
2150         do_log(SMB_VFS_OP_TRANSLATE_NAME, NT_STATUS_IS_OK(result), handle, "");
2151
2152         return result;
2153 }
2154
2155 static NTSTATUS smb_full_audit_parent_pathname(struct vfs_handle_struct *handle,
2156                                                TALLOC_CTX *mem_ctx,
2157                                                const struct smb_filename *smb_fname_in,
2158                                                struct smb_filename **parent_dir_out,
2159                                                struct smb_filename **atname_out)
2160 {
2161         NTSTATUS result;
2162
2163         result = SMB_VFS_NEXT_PARENT_PATHNAME(handle,
2164                                               mem_ctx,
2165                                               smb_fname_in,
2166                                               parent_dir_out,
2167                                               atname_out);
2168         do_log(SMB_VFS_OP_CONNECTPATH,
2169                NT_STATUS_IS_OK(result),
2170                handle,
2171                "%s",
2172                smb_fname_str_do_log(handle->conn, smb_fname_in));
2173
2174         return result;
2175 }
2176
2177 static NTSTATUS smb_full_audit_fsctl(struct vfs_handle_struct *handle,
2178                                 struct files_struct *fsp,
2179                                 TALLOC_CTX *ctx,
2180                                 uint32_t function,
2181                                 uint16_t req_flags,
2182                                 const uint8_t *_in_data,
2183                                 uint32_t in_len,
2184                                 uint8_t **_out_data,
2185                                 uint32_t max_out_len,
2186                                 uint32_t *out_len)
2187 {
2188         NTSTATUS result;
2189
2190         result = SMB_VFS_NEXT_FSCTL(handle,
2191                                 fsp,
2192                                 ctx,
2193                                 function,
2194                                 req_flags,
2195                                 _in_data,
2196                                 in_len,
2197                                 _out_data,
2198                                 max_out_len,
2199                                 out_len);
2200
2201         do_log(SMB_VFS_OP_FSCTL, NT_STATUS_IS_OK(result), handle, "");
2202
2203         return result;
2204 }
2205
2206 static struct tevent_req *smb_full_audit_offload_read_send(
2207         TALLOC_CTX *mem_ctx,
2208         struct tevent_context *ev,
2209         struct vfs_handle_struct *handle,
2210         struct files_struct *fsp,
2211         uint32_t fsctl,
2212         uint32_t ttl,
2213         off_t offset,
2214         size_t to_copy)
2215 {
2216         struct tevent_req *req = NULL;
2217
2218         req = SMB_VFS_NEXT_OFFLOAD_READ_SEND(mem_ctx, ev, handle, fsp,
2219                                              fsctl, ttl, offset, to_copy);
2220
2221         do_log(SMB_VFS_OP_OFFLOAD_READ_SEND, req, handle, "");
2222
2223         return req;
2224 }
2225
2226 static NTSTATUS smb_full_audit_offload_read_recv(
2227         struct tevent_req *req,
2228         struct vfs_handle_struct *handle,
2229         TALLOC_CTX *mem_ctx,
2230         DATA_BLOB *_token_blob)
2231 {
2232         NTSTATUS status;
2233
2234         status = SMB_VFS_NEXT_OFFLOAD_READ_RECV(req, handle, mem_ctx,
2235                                                 _token_blob);
2236
2237         do_log(SMB_VFS_OP_OFFLOAD_READ_RECV, NT_STATUS_IS_OK(status), handle, "");
2238
2239         return status;
2240 }
2241
2242 static struct tevent_req *smb_full_audit_offload_write_send(struct vfs_handle_struct *handle,
2243                                                          TALLOC_CTX *mem_ctx,
2244                                                          struct tevent_context *ev,
2245                                                          uint32_t fsctl,
2246                                                          DATA_BLOB *token,
2247                                                          off_t transfer_offset,
2248                                                          struct files_struct *dest_fsp,
2249                                                          off_t dest_off,
2250                                                             off_t num)
2251 {
2252         struct tevent_req *req;
2253
2254         req = SMB_VFS_NEXT_OFFLOAD_WRITE_SEND(handle, mem_ctx, ev,
2255                                            fsctl, token, transfer_offset,
2256                                            dest_fsp, dest_off, num);
2257
2258         do_log(SMB_VFS_OP_OFFLOAD_WRITE_SEND, req, handle, "");
2259
2260         return req;
2261 }
2262
2263 static NTSTATUS smb_full_audit_offload_write_recv(struct vfs_handle_struct *handle,
2264                                                struct tevent_req *req,
2265                                                off_t *copied)
2266 {
2267         NTSTATUS result;
2268
2269         result = SMB_VFS_NEXT_OFFLOAD_WRITE_RECV(handle, req, copied);
2270
2271         do_log(SMB_VFS_OP_OFFLOAD_WRITE_RECV, NT_STATUS_IS_OK(result), handle, "");
2272
2273         return result;
2274 }
2275
2276 static NTSTATUS smb_full_audit_fget_compression(vfs_handle_struct *handle,
2277                                                TALLOC_CTX *mem_ctx,
2278                                                struct files_struct *fsp,
2279                                                uint16_t *_compression_fmt)
2280 {
2281         NTSTATUS result;
2282
2283         result = SMB_VFS_NEXT_FGET_COMPRESSION(handle, mem_ctx, fsp,
2284                                               _compression_fmt);
2285
2286         do_log(SMB_VFS_OP_FGET_COMPRESSION, NT_STATUS_IS_OK(result), handle,
2287                "%s",
2288                fsp_str_do_log(fsp));
2289
2290         return result;
2291 }
2292
2293 static NTSTATUS smb_full_audit_set_compression(vfs_handle_struct *handle,
2294                                                TALLOC_CTX *mem_ctx,
2295                                                struct files_struct *fsp,
2296                                                uint16_t compression_fmt)
2297 {
2298         NTSTATUS result;
2299
2300         result = SMB_VFS_NEXT_SET_COMPRESSION(handle, mem_ctx, fsp,
2301                                               compression_fmt);
2302
2303         do_log(SMB_VFS_OP_SET_COMPRESSION, NT_STATUS_IS_OK(result), handle,
2304                "%s", fsp_str_do_log(fsp));
2305
2306         return result;
2307 }
2308
2309 static NTSTATUS smb_full_audit_freaddir_attr(struct vfs_handle_struct *handle,
2310                                         struct files_struct *fsp,
2311                                         TALLOC_CTX *mem_ctx,
2312                                         struct readdir_attr_data **pattr_data)
2313 {
2314         NTSTATUS status;
2315
2316         status = SMB_VFS_NEXT_FREADDIR_ATTR(handle, fsp, mem_ctx, pattr_data);
2317
2318         do_log(SMB_VFS_OP_FREADDIR_ATTR,
2319                NT_STATUS_IS_OK(status),
2320                handle,
2321                "%s",
2322                fsp_str_do_log(fsp));
2323
2324         return status;
2325 }
2326
2327 struct smb_full_audit_get_dos_attributes_state {
2328         struct vfs_aio_state aio_state;
2329         vfs_handle_struct *handle;
2330         files_struct *dir_fsp;
2331         const struct smb_filename *smb_fname;
2332         uint32_t dosmode;
2333 };
2334
2335 static void smb_full_audit_get_dos_attributes_done(struct tevent_req *subreq);
2336
2337 static struct tevent_req *smb_full_audit_get_dos_attributes_send(
2338                 TALLOC_CTX *mem_ctx,
2339                 struct tevent_context *ev,
2340                 struct vfs_handle_struct *handle,
2341                 files_struct *dir_fsp,
2342                 struct smb_filename *smb_fname)
2343 {
2344         struct tevent_req *req = NULL;
2345         struct smb_full_audit_get_dos_attributes_state *state = NULL;
2346         struct tevent_req *subreq = NULL;
2347
2348         req = tevent_req_create(mem_ctx, &state,
2349                                 struct smb_full_audit_get_dos_attributes_state);
2350         if (req == NULL) {
2351                 do_log(SMB_VFS_OP_GET_DOS_ATTRIBUTES_SEND,
2352                        false,
2353                        handle,
2354                        "%s/%s",
2355                        fsp_str_do_log(dir_fsp),
2356                        smb_fname->base_name);
2357                 return NULL;
2358         }
2359         *state = (struct smb_full_audit_get_dos_attributes_state) {
2360                 .handle = handle,
2361                 .dir_fsp = dir_fsp,
2362                 .smb_fname = smb_fname,
2363         };
2364
2365         subreq = SMB_VFS_NEXT_GET_DOS_ATTRIBUTES_SEND(mem_ctx,
2366                                                       ev,
2367                                                       handle,
2368                                                       dir_fsp,
2369                                                       smb_fname);
2370         if (tevent_req_nomem(subreq, req)) {
2371                 do_log(SMB_VFS_OP_GET_DOS_ATTRIBUTES_SEND,
2372                        false,
2373                        handle,
2374                        "%s/%s",
2375                        fsp_str_do_log(dir_fsp),
2376                        smb_fname->base_name);
2377                 return tevent_req_post(req, ev);
2378         }
2379         tevent_req_set_callback(subreq,
2380                                 smb_full_audit_get_dos_attributes_done,
2381                                 req);
2382
2383         do_log(SMB_VFS_OP_GET_DOS_ATTRIBUTES_SEND,
2384                true,
2385                handle,
2386                "%s/%s",
2387                fsp_str_do_log(dir_fsp),
2388                smb_fname->base_name);
2389
2390         return req;
2391 }
2392
2393 static void smb_full_audit_get_dos_attributes_done(struct tevent_req *subreq)
2394 {
2395         struct tevent_req *req =
2396                 tevent_req_callback_data(subreq,
2397                 struct tevent_req);
2398         struct smb_full_audit_get_dos_attributes_state *state =
2399                 tevent_req_data(req,
2400                 struct smb_full_audit_get_dos_attributes_state);
2401         NTSTATUS status;
2402
2403         status = SMB_VFS_NEXT_GET_DOS_ATTRIBUTES_RECV(subreq,
2404                                                       &state->aio_state,
2405                                                       &state->dosmode);
2406         TALLOC_FREE(subreq);
2407         if (tevent_req_nterror(req, status)) {
2408                 return;
2409         }
2410
2411         tevent_req_done(req);
2412         return;
2413 }
2414
2415 static NTSTATUS smb_full_audit_get_dos_attributes_recv(struct tevent_req *req,
2416                                                 struct vfs_aio_state *aio_state,
2417                                                 uint32_t *dosmode)
2418 {
2419         struct smb_full_audit_get_dos_attributes_state *state =
2420                 tevent_req_data(req,
2421                 struct smb_full_audit_get_dos_attributes_state);
2422         NTSTATUS status;
2423
2424         if (tevent_req_is_nterror(req, &status)) {
2425                 do_log(SMB_VFS_OP_GET_DOS_ATTRIBUTES_RECV,
2426                        false,
2427                        state->handle,
2428                        "%s/%s",
2429                        fsp_str_do_log(state->dir_fsp),
2430                        state->smb_fname->base_name);
2431                 tevent_req_received(req);
2432                 return status;
2433         }
2434
2435         do_log(SMB_VFS_OP_GET_DOS_ATTRIBUTES_RECV,
2436                true,
2437                state->handle,
2438                "%s/%s",
2439                fsp_str_do_log(state->dir_fsp),
2440                state->smb_fname->base_name);
2441
2442         *aio_state = state->aio_state;
2443         *dosmode = state->dosmode;
2444         tevent_req_received(req);
2445         return NT_STATUS_OK;
2446 }
2447
2448 static NTSTATUS smb_full_audit_fget_dos_attributes(
2449                                 struct vfs_handle_struct *handle,
2450                                 struct files_struct *fsp,
2451                                 uint32_t *dosmode)
2452 {
2453         NTSTATUS status;
2454
2455         status = SMB_VFS_NEXT_FGET_DOS_ATTRIBUTES(handle,
2456                                 fsp,
2457                                 dosmode);
2458
2459         do_log(SMB_VFS_OP_FGET_DOS_ATTRIBUTES,
2460                 NT_STATUS_IS_OK(status),
2461                 handle,
2462                 "%s",
2463                 fsp_str_do_log(fsp));
2464
2465         return status;
2466 }
2467
2468 static NTSTATUS smb_full_audit_fset_dos_attributes(
2469                                 struct vfs_handle_struct *handle,
2470                                 struct files_struct *fsp,
2471                                 uint32_t dosmode)
2472 {
2473         NTSTATUS status;
2474
2475         status = SMB_VFS_NEXT_FSET_DOS_ATTRIBUTES(handle,
2476                                 fsp,
2477                                 dosmode);
2478
2479         do_log(SMB_VFS_OP_FSET_DOS_ATTRIBUTES,
2480                 NT_STATUS_IS_OK(status),
2481                 handle,
2482                 "%s",
2483                 fsp_str_do_log(fsp));
2484
2485         return status;
2486 }
2487
2488 static NTSTATUS smb_full_audit_fget_nt_acl(vfs_handle_struct *handle, files_struct *fsp,
2489                                            uint32_t security_info,
2490                                            TALLOC_CTX *mem_ctx,
2491                                            struct security_descriptor **ppdesc)
2492 {
2493         NTSTATUS result;
2494
2495         result = SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp, security_info,
2496                                           mem_ctx, ppdesc);
2497
2498         do_log(SMB_VFS_OP_FGET_NT_ACL, NT_STATUS_IS_OK(result), handle,
2499                "%s", fsp_str_do_log(fsp));
2500
2501         return result;
2502 }
2503
2504 static NTSTATUS smb_full_audit_fset_nt_acl(vfs_handle_struct *handle, files_struct *fsp,
2505                               uint32_t security_info_sent,
2506                               const struct security_descriptor *psd)
2507 {
2508         struct vfs_full_audit_private_data *pd;
2509         NTSTATUS result;
2510         char *sd = NULL;
2511
2512         SMB_VFS_HANDLE_GET_DATA(handle, pd,
2513                                 struct vfs_full_audit_private_data,
2514                                 return NT_STATUS_INTERNAL_ERROR);
2515
2516         if (pd->log_secdesc) {
2517                 sd = sddl_encode(talloc_tos(), psd, get_global_sam_sid());
2518         }
2519
2520         result = SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp, security_info_sent, psd);
2521
2522         do_log(SMB_VFS_OP_FSET_NT_ACL, NT_STATUS_IS_OK(result), handle,
2523                "%s [%s]", fsp_str_do_log(fsp), sd ? sd : "");
2524
2525         TALLOC_FREE(sd);
2526
2527         return result;
2528 }
2529
2530 static NTSTATUS smb_full_audit_audit_file(struct vfs_handle_struct *handle,
2531                                 struct smb_filename *file,
2532                                 struct security_acl *sacl,
2533                                 uint32_t access_requested,
2534                                 uint32_t access_denied)
2535 {
2536         NTSTATUS result;
2537
2538         result = SMB_VFS_NEXT_AUDIT_FILE(handle,
2539                                         file,
2540                                         sacl,
2541                                         access_requested,
2542                                         access_denied);
2543
2544         do_log(SMB_VFS_OP_AUDIT_FILE, NT_STATUS_IS_OK(result), handle,
2545                         "%s",
2546                         smb_fname_str_do_log(handle->conn, file));
2547
2548         return result;
2549 }
2550
2551 static SMB_ACL_T smb_full_audit_sys_acl_get_fd(vfs_handle_struct *handle,
2552                                                files_struct *fsp,
2553                                                SMB_ACL_TYPE_T type,
2554                                                TALLOC_CTX *mem_ctx)
2555 {
2556         SMB_ACL_T result;
2557
2558         result = SMB_VFS_NEXT_SYS_ACL_GET_FD(handle,
2559                                              fsp,
2560                                              type,
2561                                              mem_ctx);
2562
2563         do_log(SMB_VFS_OP_SYS_ACL_GET_FD, (result != NULL), handle,
2564                "%s", fsp_str_do_log(fsp));
2565
2566         return result;
2567 }
2568
2569 static int smb_full_audit_sys_acl_blob_get_fd(vfs_handle_struct *handle,
2570                                               files_struct *fsp,
2571                                               TALLOC_CTX *mem_ctx,
2572                                               char **blob_description,
2573                                               DATA_BLOB *blob)
2574 {
2575         int result;
2576
2577         result = SMB_VFS_NEXT_SYS_ACL_BLOB_GET_FD(handle, fsp, mem_ctx, blob_description, blob);
2578
2579         do_log(SMB_VFS_OP_SYS_ACL_BLOB_GET_FD, (result >= 0), handle,
2580                "%s", fsp_str_do_log(fsp));
2581
2582         return result;
2583 }
2584
2585 static int smb_full_audit_sys_acl_set_fd(vfs_handle_struct *handle,
2586                                          struct files_struct *fsp,
2587                                          SMB_ACL_TYPE_T type,
2588                                          SMB_ACL_T theacl)
2589 {
2590         int result;
2591
2592         result = SMB_VFS_NEXT_SYS_ACL_SET_FD(handle, fsp, type, theacl);
2593
2594         do_log(SMB_VFS_OP_SYS_ACL_SET_FD, (result >= 0), handle,
2595                "%s", fsp_str_do_log(fsp));
2596
2597         return result;
2598 }
2599
2600 static int smb_full_audit_sys_acl_delete_def_fd(vfs_handle_struct *handle,
2601                                 struct files_struct *fsp)
2602 {
2603         int result;
2604
2605         result = SMB_VFS_NEXT_SYS_ACL_DELETE_DEF_FD(handle, fsp);
2606
2607         do_log(SMB_VFS_OP_SYS_ACL_DELETE_DEF_FD,
2608                (result >= 0),
2609                handle,
2610                "%s",
2611                fsp_str_do_log(fsp));
2612
2613         return result;
2614 }
2615
2616 static ssize_t smb_full_audit_getxattr(struct vfs_handle_struct *handle,
2617                               const struct smb_filename *smb_fname,
2618                               const char *name, void *value, size_t size)
2619 {
2620         ssize_t result;
2621
2622         result = SMB_VFS_NEXT_GETXATTR(handle, smb_fname, name, value, size);
2623
2624         do_log(SMB_VFS_OP_GETXATTR,
2625                (result >= 0),
2626                handle,
2627                "%s|%s",
2628                smb_fname_str_do_log(handle->conn, smb_fname),
2629                name);
2630
2631         return result;
2632 }
2633
2634 struct smb_full_audit_getxattrat_state {
2635         struct vfs_aio_state aio_state;
2636         vfs_handle_struct *handle;
2637         files_struct *dir_fsp;
2638         const struct smb_filename *smb_fname;
2639         const char *xattr_name;
2640         ssize_t xattr_size;
2641         uint8_t *xattr_value;
2642 };
2643
2644 static void smb_full_audit_getxattrat_done(struct tevent_req *subreq);
2645
2646 static struct tevent_req *smb_full_audit_getxattrat_send(
2647                         TALLOC_CTX *mem_ctx,
2648                         struct tevent_context *ev,
2649                         struct vfs_handle_struct *handle,
2650                         files_struct *dir_fsp,
2651                         const struct smb_filename *smb_fname,
2652                         const char *xattr_name,
2653                         size_t alloc_hint)
2654 {
2655         struct tevent_req *req = NULL;
2656         struct tevent_req *subreq = NULL;
2657         struct smb_full_audit_getxattrat_state *state = NULL;
2658
2659         req = tevent_req_create(mem_ctx, &state,
2660                                 struct smb_full_audit_getxattrat_state);
2661         if (req == NULL) {
2662                 do_log(SMB_VFS_OP_GETXATTRAT_SEND,
2663                        false,
2664                        handle,
2665                        "%s/%s|%s",
2666                        fsp_str_do_log(dir_fsp),
2667                        smb_fname->base_name,
2668                        xattr_name);
2669                 return NULL;
2670         }
2671         *state = (struct smb_full_audit_getxattrat_state) {
2672                 .handle = handle,
2673                 .dir_fsp = dir_fsp,
2674                 .smb_fname = smb_fname,
2675                 .xattr_name = xattr_name,
2676         };
2677
2678         subreq = SMB_VFS_NEXT_GETXATTRAT_SEND(state,
2679                                               ev,
2680                                               handle,
2681                                               dir_fsp,
2682                                               smb_fname,
2683                                               xattr_name,
2684                                               alloc_hint);
2685         if (tevent_req_nomem(subreq, req)) {
2686                 do_log(SMB_VFS_OP_GETXATTRAT_SEND,
2687                        false,
2688                        handle,
2689                        "%s/%s|%s",
2690                        fsp_str_do_log(dir_fsp),
2691                        smb_fname->base_name,
2692                        xattr_name);
2693                 return tevent_req_post(req, ev);
2694         }
2695         tevent_req_set_callback(subreq, smb_full_audit_getxattrat_done, req);
2696
2697         do_log(SMB_VFS_OP_GETXATTRAT_SEND,
2698                true,
2699                handle,
2700                "%s/%s|%s",
2701                fsp_str_do_log(dir_fsp),
2702                smb_fname->base_name,
2703                xattr_name);
2704
2705         return req;
2706 }
2707
2708 static void smb_full_audit_getxattrat_done(struct tevent_req *subreq)
2709 {
2710         struct tevent_req *req = tevent_req_callback_data(
2711                 subreq, struct tevent_req);
2712         struct smb_full_audit_getxattrat_state *state = tevent_req_data(
2713                 req, struct smb_full_audit_getxattrat_state);
2714
2715         state->xattr_size = SMB_VFS_NEXT_GETXATTRAT_RECV(subreq,
2716                                                          &state->aio_state,
2717                                                          state,
2718                                                          &state->xattr_value);
2719         TALLOC_FREE(subreq);
2720         if (state->xattr_size == -1) {
2721                 tevent_req_error(req, state->aio_state.error);
2722                 return;
2723         }
2724
2725         tevent_req_done(req);
2726 }
2727
2728 static ssize_t smb_full_audit_getxattrat_recv(struct tevent_req *req,
2729                                               struct vfs_aio_state *aio_state,
2730                                               TALLOC_CTX *mem_ctx,
2731                                               uint8_t **xattr_value)
2732 {
2733         struct smb_full_audit_getxattrat_state *state = tevent_req_data(
2734                 req, struct smb_full_audit_getxattrat_state);
2735         ssize_t xattr_size;
2736
2737         if (tevent_req_is_unix_error(req, &aio_state->error)) {
2738                 do_log(SMB_VFS_OP_GETXATTRAT_RECV,
2739                        false,
2740                        state->handle,
2741                        "%s/%s|%s",
2742                        fsp_str_do_log(state->dir_fsp),
2743                        state->smb_fname->base_name,
2744                        state->xattr_name);
2745                 tevent_req_received(req);
2746                 return -1;
2747         }
2748
2749         do_log(SMB_VFS_OP_GETXATTRAT_RECV,
2750                true,
2751                state->handle,
2752                "%s/%s|%s",
2753                fsp_str_do_log(state->dir_fsp),
2754                state->smb_fname->base_name,
2755                state->xattr_name);
2756
2757         *aio_state = state->aio_state;
2758         xattr_size = state->xattr_size;
2759         if (xattr_value != NULL) {
2760                 *xattr_value = talloc_move(mem_ctx, &state->xattr_value);
2761         }
2762
2763         tevent_req_received(req);
2764         return xattr_size;
2765 }
2766
2767 static ssize_t smb_full_audit_fgetxattr(struct vfs_handle_struct *handle,
2768                                struct files_struct *fsp,
2769                                const char *name, void *value, size_t size)
2770 {
2771         ssize_t result;
2772
2773         result = SMB_VFS_NEXT_FGETXATTR(handle, fsp, name, value, size);
2774
2775         do_log(SMB_VFS_OP_FGETXATTR, (result >= 0), handle,
2776                "%s|%s", fsp_str_do_log(fsp), name);
2777
2778         return result;
2779 }
2780
2781 static ssize_t smb_full_audit_flistxattr(struct vfs_handle_struct *handle,
2782                                 struct files_struct *fsp, char *list,
2783                                 size_t size)
2784 {
2785         ssize_t result;
2786
2787         result = SMB_VFS_NEXT_FLISTXATTR(handle, fsp, list, size);
2788
2789         do_log(SMB_VFS_OP_FLISTXATTR, (result >= 0), handle,
2790                "%s", fsp_str_do_log(fsp));
2791
2792         return result;
2793 }
2794
2795 static int smb_full_audit_fremovexattr(struct vfs_handle_struct *handle,
2796                               struct files_struct *fsp,
2797                               const char *name)
2798 {
2799         int result;
2800
2801         result = SMB_VFS_NEXT_FREMOVEXATTR(handle, fsp, name);
2802
2803         do_log(SMB_VFS_OP_FREMOVEXATTR, (result >= 0), handle,
2804                "%s|%s", fsp_str_do_log(fsp), name);
2805
2806         return result;
2807 }
2808
2809 static int smb_full_audit_fsetxattr(struct vfs_handle_struct *handle,
2810                            struct files_struct *fsp, const char *name,
2811                            const void *value, size_t size, int flags)
2812 {
2813         int result;
2814
2815         result = SMB_VFS_NEXT_FSETXATTR(handle, fsp, name, value, size, flags);
2816
2817         do_log(SMB_VFS_OP_FSETXATTR, (result >= 0), handle,
2818                "%s|%s", fsp_str_do_log(fsp), name);
2819
2820         return result;
2821 }
2822
2823 static bool smb_full_audit_aio_force(struct vfs_handle_struct *handle,
2824                                      struct files_struct *fsp)
2825 {
2826         bool result;
2827
2828         result = SMB_VFS_NEXT_AIO_FORCE(handle, fsp);
2829         do_log(SMB_VFS_OP_AIO_FORCE, result, handle,
2830                 "%s", fsp_str_do_log(fsp));
2831
2832         return result;
2833 }
2834
2835 static NTSTATUS smb_full_audit_durable_cookie(struct vfs_handle_struct *handle,
2836                                 struct files_struct *fsp,
2837                                 TALLOC_CTX *mem_ctx,
2838                                 DATA_BLOB *cookie)
2839 {
2840         NTSTATUS result;
2841
2842         result = SMB_VFS_NEXT_DURABLE_COOKIE(handle,
2843                                         fsp,
2844                                         mem_ctx,
2845                                         cookie);
2846
2847         do_log(SMB_VFS_OP_DURABLE_COOKIE, NT_STATUS_IS_OK(result), handle,
2848                         "%s", fsp_str_do_log(fsp));
2849
2850         return result;
2851 }
2852
2853 static NTSTATUS smb_full_audit_durable_disconnect(
2854                                 struct vfs_handle_struct *handle,
2855                                 struct files_struct *fsp,
2856                                 const DATA_BLOB old_cookie,
2857                                 TALLOC_CTX *mem_ctx,
2858                                 DATA_BLOB *new_cookie)
2859 {
2860         NTSTATUS result;
2861
2862         result = SMB_VFS_NEXT_DURABLE_DISCONNECT(handle,
2863                                         fsp,
2864                                         old_cookie,
2865                                         mem_ctx,
2866                                         new_cookie);
2867
2868         do_log(SMB_VFS_OP_DURABLE_DISCONNECT, NT_STATUS_IS_OK(result), handle,
2869                         "%s", fsp_str_do_log(fsp));
2870
2871         return result;
2872 }
2873
2874 static NTSTATUS smb_full_audit_durable_reconnect(
2875                                 struct vfs_handle_struct *handle,
2876                                 struct smb_request *smb1req,
2877                                 struct smbXsrv_open *op,
2878                                 const DATA_BLOB old_cookie,
2879                                 TALLOC_CTX *mem_ctx,
2880                                 struct files_struct **fsp,
2881                                 DATA_BLOB *new_cookie)
2882 {
2883         NTSTATUS result;
2884
2885         result = SMB_VFS_NEXT_DURABLE_RECONNECT(handle,
2886                                         smb1req,
2887                                         op,
2888                                         old_cookie,
2889                                         mem_ctx,
2890                                         fsp,
2891                                         new_cookie);
2892
2893         do_log(SMB_VFS_OP_DURABLE_RECONNECT,
2894                         NT_STATUS_IS_OK(result),
2895                         handle,
2896                         "");
2897
2898         return result;
2899 }
2900
2901 static struct vfs_fn_pointers vfs_full_audit_fns = {
2902
2903         /* Disk operations */
2904
2905         .connect_fn = smb_full_audit_connect,
2906         .disconnect_fn = smb_full_audit_disconnect,
2907         .disk_free_fn = smb_full_audit_disk_free,
2908         .get_quota_fn = smb_full_audit_get_quota,
2909         .set_quota_fn = smb_full_audit_set_quota,
2910         .get_shadow_copy_data_fn = smb_full_audit_get_shadow_copy_data,
2911         .statvfs_fn = smb_full_audit_statvfs,
2912         .fs_capabilities_fn = smb_full_audit_fs_capabilities,
2913         .get_dfs_referrals_fn = smb_full_audit_get_dfs_referrals,
2914         .create_dfs_pathat_fn = smb_full_audit_create_dfs_pathat,
2915         .read_dfs_pathat_fn = smb_full_audit_read_dfs_pathat,
2916         .fdopendir_fn = smb_full_audit_fdopendir,
2917         .readdir_fn = smb_full_audit_readdir,
2918         .seekdir_fn = smb_full_audit_seekdir,
2919         .telldir_fn = smb_full_audit_telldir,
2920         .rewind_dir_fn = smb_full_audit_rewinddir,
2921         .mkdirat_fn = smb_full_audit_mkdirat,
2922         .closedir_fn = smb_full_audit_closedir,
2923         .openat_fn = smb_full_audit_openat,
2924         .create_file_fn = smb_full_audit_create_file,
2925         .close_fn = smb_full_audit_close,
2926         .pread_fn = smb_full_audit_pread,
2927         .pread_send_fn = smb_full_audit_pread_send,
2928         .pread_recv_fn = smb_full_audit_pread_recv,
2929         .pwrite_fn = smb_full_audit_pwrite,
2930         .pwrite_send_fn = smb_full_audit_pwrite_send,
2931         .pwrite_recv_fn = smb_full_audit_pwrite_recv,
2932         .lseek_fn = smb_full_audit_lseek,
2933         .sendfile_fn = smb_full_audit_sendfile,
2934         .recvfile_fn = smb_full_audit_recvfile,
2935         .renameat_fn = smb_full_audit_renameat,
2936         .fsync_send_fn = smb_full_audit_fsync_send,
2937         .fsync_recv_fn = smb_full_audit_fsync_recv,
2938         .stat_fn = smb_full_audit_stat,
2939         .fstat_fn = smb_full_audit_fstat,
2940         .lstat_fn = smb_full_audit_lstat,
2941         .get_alloc_size_fn = smb_full_audit_get_alloc_size,
2942         .unlinkat_fn = smb_full_audit_unlinkat,
2943         .fchmod_fn = smb_full_audit_fchmod,
2944         .fchown_fn = smb_full_audit_fchown,
2945         .lchown_fn = smb_full_audit_lchown,
2946         .chdir_fn = smb_full_audit_chdir,
2947         .getwd_fn = smb_full_audit_getwd,
2948         .fntimes_fn = smb_full_audit_fntimes,
2949         .ftruncate_fn = smb_full_audit_ftruncate,
2950         .fallocate_fn = smb_full_audit_fallocate,
2951         .lock_fn = smb_full_audit_lock,
2952         .kernel_flock_fn = smb_full_audit_kernel_flock,
2953         .fcntl_fn = smb_full_audit_fcntl,
2954         .linux_setlease_fn = smb_full_audit_linux_setlease,
2955         .getlock_fn = smb_full_audit_getlock,
2956         .symlinkat_fn = smb_full_audit_symlinkat,
2957         .readlinkat_fn = smb_full_audit_readlinkat,
2958         .linkat_fn = smb_full_audit_linkat,
2959         .mknodat_fn = smb_full_audit_mknodat,
2960         .realpath_fn = smb_full_audit_realpath,
2961         .chflags_fn = smb_full_audit_chflags,
2962         .file_id_create_fn = smb_full_audit_file_id_create,
2963         .fs_file_id_fn = smb_full_audit_fs_file_id,
2964         .offload_read_send_fn = smb_full_audit_offload_read_send,
2965         .offload_read_recv_fn = smb_full_audit_offload_read_recv,
2966         .offload_write_send_fn = smb_full_audit_offload_write_send,
2967         .offload_write_recv_fn = smb_full_audit_offload_write_recv,
2968         .fget_compression_fn = smb_full_audit_fget_compression,
2969         .set_compression_fn = smb_full_audit_set_compression,
2970         .snap_check_path_fn =  smb_full_audit_snap_check_path,
2971         .snap_create_fn = smb_full_audit_snap_create,
2972         .snap_delete_fn = smb_full_audit_snap_delete,
2973         .fstreaminfo_fn = smb_full_audit_fstreaminfo,
2974         .get_real_filename_fn = smb_full_audit_get_real_filename,
2975         .connectpath_fn = smb_full_audit_connectpath,
2976         .brl_lock_windows_fn = smb_full_audit_brl_lock_windows,
2977         .brl_unlock_windows_fn = smb_full_audit_brl_unlock_windows,
2978         .strict_lock_check_fn = smb_full_audit_strict_lock_check,
2979         .translate_name_fn = smb_full_audit_translate_name,
2980         .parent_pathname_fn = smb_full_audit_parent_pathname,
2981         .fsctl_fn = smb_full_audit_fsctl,
2982         .get_dos_attributes_send_fn = smb_full_audit_get_dos_attributes_send,
2983         .get_dos_attributes_recv_fn = smb_full_audit_get_dos_attributes_recv,
2984         .fget_dos_attributes_fn = smb_full_audit_fget_dos_attributes,
2985         .fset_dos_attributes_fn = smb_full_audit_fset_dos_attributes,
2986         .fget_nt_acl_fn = smb_full_audit_fget_nt_acl,
2987         .fset_nt_acl_fn = smb_full_audit_fset_nt_acl,
2988         .audit_file_fn = smb_full_audit_audit_file,
2989         .sys_acl_get_fd_fn = smb_full_audit_sys_acl_get_fd,
2990         .sys_acl_blob_get_fd_fn = smb_full_audit_sys_acl_blob_get_fd,
2991         .sys_acl_set_fd_fn = smb_full_audit_sys_acl_set_fd,
2992         .sys_acl_delete_def_fd_fn = smb_full_audit_sys_acl_delete_def_fd,
2993         .getxattr_fn = smb_full_audit_getxattr,
2994         .getxattrat_send_fn = smb_full_audit_getxattrat_send,
2995         .getxattrat_recv_fn = smb_full_audit_getxattrat_recv,
2996         .fgetxattr_fn = smb_full_audit_fgetxattr,
2997         .flistxattr_fn = smb_full_audit_flistxattr,
2998         .fremovexattr_fn = smb_full_audit_fremovexattr,
2999         .fsetxattr_fn = smb_full_audit_fsetxattr,
3000         .aio_force_fn = smb_full_audit_aio_force,
3001         .durable_cookie_fn = smb_full_audit_durable_cookie,
3002         .durable_disconnect_fn = smb_full_audit_durable_disconnect,
3003         .durable_reconnect_fn = smb_full_audit_durable_reconnect,
3004         .freaddir_attr_fn = smb_full_audit_freaddir_attr,
3005 };
3006
3007 static_decl_vfs;
3008 NTSTATUS vfs_full_audit_init(TALLOC_CTX *ctx)
3009 {
3010         NTSTATUS ret;
3011
3012         smb_vfs_assert_all_fns(&vfs_full_audit_fns, "full_audit");
3013
3014         ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "full_audit",
3015                                &vfs_full_audit_fns);
3016
3017         if (!NT_STATUS_IS_OK(ret))
3018                 return ret;
3019
3020         vfs_full_audit_debug_level = debug_add_class("full_audit");
3021         if (vfs_full_audit_debug_level == -1) {
3022                 vfs_full_audit_debug_level = DBGC_VFS;
3023                 DEBUG(0, ("vfs_full_audit: Couldn't register custom debugging "
3024                           "class!\n"));
3025         } else {
3026                 DEBUG(10, ("vfs_full_audit: Debug class number of "
3027                            "'full_audit': %d\n", vfs_full_audit_debug_level));
3028         }
3029         
3030         return ret;
3031 }