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