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