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