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