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