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